import { extend } from "umi-request"
import {history} from '@@/core/history';
import {stringify} from 'querystring'
import { message } from 'antd';

const request = extend({
  credentials: "include",
  // prefix: process.env.NODE_ENV === "production" ? "http://user-backend.academicnav.cn" : undefined,
  // prefix: process.env.NODE_ENV === "production" ? "http://www.academicnav.cn" : undefined,
  prefix: process.env.NODE_ENV === "production" ? "http://8.134.201.97" : undefined,
});

request.interceptors.request.use((url,option): any=>{
  return{
    url,
    options:{
      ...option,
      headers:{
      },
    },
  };
});

request.interceptors.response.use(async (response,options): Promise<any> => {
  const res = await response.clone().json();
  if(res.code === 0){
    return res.data;
  }
  if(res.code === 40100){
    message.error("请先登录");
    history.replace({
      pathname:"/user/login",
      search:stringify({
        redirect:location.pathname,
      }),
    });
  } else{
    message.error("账号格式错误")
  }
  return res.data;
});

export default request;

